
package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;

/********* References:
 * 
 * http://stackoverflow.com/questions/16228502/display-items-from-a-database-in-a-javafx-tableview
 * 
 * http://stackoverflow.com/questions/18497699/populate-a-tableview-using-database-in-javafx
 * 
 * @author ukhan
 *
 **********/

public class PreGameScreenController implements Initializable , ControlledScreen {

	// Variables for reading input from the form
	@FXML
	private Text preGameMessage;
  
	/* 
    @FXML
    ObservableList<PreGameScreenPlayerRanking> rankingData;
    */
    // This is for representing the game list
    @FXML
    private ListView<String> gameList;
        

    // This is for representing table of player rankings
    @FXML
    private TableView<PreGameScreenPlayerRanking> playerStatsTable = new TableView<PreGameScreenPlayerRanking>();
    // private ObservableList<PreGameScreenPlayerRanking> playerStatsData;
  
    @FXML
    private TableColumn<PreGameScreenPlayerRanking, String> colPlayerName;
    @FXML
    private TableColumn<PreGameScreenPlayerRanking, int[]> colPlayerScore;
    

    UserSession sessionX = UserSession.getInstance();
    PreGameScreenSession sessionG = PreGameScreenSession.getInstance();

    String userName;
    char[] passWord;
 
	ScreensController myController;
	
	
    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
    	
      /*
    	// Add a listener to listen for mous selection from the game list
    	gameList.setOnMouseClicked(new EventHandler <MouseEvent>(){
 
          @Override
          public void handle(MouseEvent arg0) {
             
        	  preGameMessage.setText("Selected: " +
            		  gameList.getSelectionModel().getSelectedItems());
          }
 
        });
      */
    	
		// Init ListView and listen for selection changes
		gameList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable,
					String oldValue, String newValue) {
				
				// preGameMessage.setText("Game was  Selection Changed (newValue: " + newValue + ")\n");
				preGameMessage.setText(newValue + "was selected to join");
				
			}
		});
    	
        /********
         * Show a list of available games
         * 
    	 ********/
        System.out.println("Printing list of available games");
        sessionG.printList();
        
        // Print the list of available games
        gameList.setItems(FXCollections.observableList(sessionG.myAvailableGames));

        // Show instructions to the users to select a game or start a new game
        preGameMessage.setText("    From the list above please select a game and push"
        		+ "\"Join Game\" Button \n    or push the button \"Create Game\" to create "
        		+ "a new game\n\n");
		
        assert playerStatsTable != null : "fx:id=\"tableview\" was not injected: check your FXML file 'PreGameScreen.fxml'.";
        colPlayerName.setCellValueFactory(new PropertyValueFactory<PreGameScreenPlayerRanking, String>("playerName"));
        colPlayerScore.setCellValueFactory(new PropertyValueFactory<PreGameScreenPlayerRanking, int[]>("playerScore"));

		try {
			buildRankingData();
		} catch (Exception exception) {
			exception.printStackTrace();
		}	
    }
    
	public void buildRankingData()
	{
	 /*
		//ObservableList<Object> dataArray = FXCollections.observableArrayList();
		data = FXCollections.observableArrayList();
		PreGameScreenPlayerRanking dataSet = new PreGameScreenPlayerRanking();
		dataSet.setPlayerName("John Doe");
		dataSet.setPlayerScore(100);
		data.add(dataSet);
		playerStatsTable.setItems(data);

		final ObservableList<PreGameScreenPlayerRanking> statTable =  FXCollections.observableArrayList(
    		new PreGameScreenPlayerRanking("Jacob", 100),
    		new PreGameScreenPlayerRanking("Robert", 100),
    		new PreGameScreenPlayerRanking("Mira", 100),
    		new PreGameScreenPlayerRanking("Mark", 100)
		);

		 playerStatsData =  FXCollections.observableArrayList();
		 
		 PreGameScreenPlayerRanking playerInfo = new PreGameScreenPlayerRanking("joe",100);
		 
		 // playerInfo.playerName.set("Joe");
		 // playerInfo.playerScore.set(100);
		
		 playerStatsData.add(playerInfo);
		 playerStatsTable.setItems(playerStatsData);

		 playerStatsData = FXCollections.observableArrayList( 
    		new PreGameScreenPlayerRanking("Jacob", 100),
    		new PreGameScreenPlayerRanking("Robert", 100),
    		new PreGameScreenPlayerRanking("Mira", 100),
    		new PreGameScreenPlayerRanking("Mark", 100)
		);
		
		playerStatsTable.setItems(playerStatsData);
		
	*/
	/*
		sessionG.playerStatsData = FXCollections.observableArrayList( 
    		new PreGameScreenPlayerRanking("Jacob", 100),
    		new PreGameScreenPlayerRanking("Robert", 100),
    		new PreGameScreenPlayerRanking("Mira", 100),
    		new PreGameScreenPlayerRanking("Mark", 100)
		);
    */
		if(sessionG.playerStatsData.isEmpty()) {
			System.out.println("sessionG.playerStatsData is empty");
		}
		else {
			playerStatsTable.setItems(sessionG.playerStatsData);
			playerStatsTable.setVisible(true);
		}
	}
    
    
    public void setScreenParent(ScreensController screenParent){
        myController = screenParent;
    }
    
    /*
     * Call this method when when the "Create Game" button is pushed
     * 
     */
    @FXML
    private void processCreateNewGameRequest(ActionEvent event){
       // myController.setScreen(ScreensFramework.RegistrationScreenID);
       preGameMessage.setText("    Creating a new game for you");
       // System.out.println("Game creation request received");

    }
    
    @FXML 
    private void processJoinGameRequest(ActionEvent event) {
    	String gameToJoin = gameList.getSelectionModel().getSelectedItem();
    	// System.out.println("clicked on " + gameList.getSelectionModel().getSelectedItem());
    	System.out.println("You requested to join: "+ gameToJoin);
    	if(gameToJoin != null) {
    		System.out.println("Processing your request");
    		sessionG.requestedGameToJoin = gameToJoin;
			myController.setScreen(ScreensFramework.GameScreenID);

    	}
    }
}
